package de.derhess.projects.air_MaxFlashHarmony
{
	import com.demonsters.debugger.MonsterDebugger;
	
	import de.derhess.drawing.brushes.RibbonBrush;
	import de.derhess.drawing.brushes.SimpleBrush;
	import de.derhess.drawing.brushes.SquaresBrush;
	
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.tuio.connectors.UDPConnector;
	import org.tuio.osc.OSCManager;
	import org.tuio.osc.OSCMessage;

	[SWF(width="1200", height="700", frameRate="30", backgroundColor="#FFFFFF")]
	public class as3_MaxFlashHarmony extends Sprite implements IHarmonyApp
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var oscListener:HarmonyOSCListener;
		private var oscManager:OSCManager;
		private var receiver:UDPConnector;
		private var sender:UDPConnector;
		
		private var canvas:HarmonyCanvas
		
		private var voiceX:int;
		private var voiceY:int;
		private var violineX:int;
		private var violineY:int;
		
		private var voiceBrush:RibbonBrush;
		private var violineBrush:RibbonBrush;
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function as3_MaxFlashHarmony()
		{
			super();
			// Air stuff
			this.stage.nativeWindow.visible = true;
			NativeApplication.nativeApplication.addEventListener(Event.EXITING,handleExiting); 
			
			// Start Debugging
			MonsterDebugger.initialize(this);
			MonsterDebugger.trace(this, "Hello Air!");
			
			// GUI
			canvas = new HarmonyCanvas(1200,700);
			addChild(canvas);
			voiceBrush = new RibbonBrush(canvas.drawing,this.stage.width/2,this.stage.height/2);
			voiceBrush.style.color = 0xDD0000;
			voiceBrush.strokeStart(this.stage.width/2,this.stage.height/2);
			violineBrush = new RibbonBrush(canvas.drawing,this.stage.width/2,this.stage.height/2);
			violineBrush.style.color = 0x00DD00;
			violineBrush.strokeStart(this.stage.width/2,this.stage.height/2);
			
			// Set up OSC 
			oscListener = new HarmonyOSCListener();
			oscListener.app = this;
			receiver = new UDPConnector("127.0.0.1",9001,true);
			sender = new UDPConnector("127.0.0.1", 9000, false);
			oscManager = new OSCManager(receiver, sender,false);
			oscManager.addMsgListener(oscListener)
			oscManager.start();
		}
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		private function renderDrawing():void 
		{
			// save the drawing in a bitmapData Object for performance
			canvas.bmpCanvas.draw(canvas.drawing);
			// clean the canvas again for new drawings. Too much drawing decrease the performance very fast
			canvas.drawing.graphics.clear();
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		public function updateOSCData(msg:OSCMessage):void 
		{
			if(msg.address == "/cursor/voice") {
				//trace("draw voice brush " + msg.arguments[0] + "/" + msg.arguments[1]);
				if(voiceX != msg.arguments[0] || voiceY != msg.arguments[1]) {
					voiceX = int(msg.arguments[0]);
					voiceY = int(msg.arguments[1]);
					//trace("Voice: " + voiceX + " / " + voiceY);
					voiceBrush.stroke(voiceX,voiceY);
				}
			}
			
			if(msg.address == "/cursor/violine") {
				//trace("draw voice brush " + msg.arguments[0] + "/" + msg.arguments[1]);
				if(violineX != msg.arguments[0] || violineY != msg.arguments[1]) {
					violineX = int(msg.arguments[0]);
					violineY = int(msg.arguments[1]);
					//trace("Violine: " + violineX + " / " + violineY);
					violineBrush.stroke(violineX,violineY);
				}
			}
			
			// Appears when the musician makes a big change in the style of playing the instrument
			if(msg.address == "/attack/voice" || msg.address == "/attack/violine") {
				MonsterDebugger.trace(this,"ATTACK!!");
			}
			
			renderDrawing();
		}
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------

		private function handleExiting(event:Event):void
		{
			MonsterDebugger.trace(this,"exit Application");
			receiver.close();
			oscManager.stop();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
	}
}